/**
 * 
 */

package com.chengyu.eyc.controller;

import java.io.IOException;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chengyu.eyc.constant.EycConstants;
import com.chengyu.eyc.model.EycUser;
import com.chengyu.eyc.utils.VerifyCodeUtil;

/**
 * @author kevin
 *
 */
@Controller
@RequestMapping
public class HomeController {

    @ResponseBody
    @GetMapping(value = "/verifyCode")
    public void genVerifyCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");
        // 生成随机字串
        // 存入会话session
        HttpSession session = request.getSession(true);
        String verifyCode = VerifyCodeUtil.generateVerifyCode(6);
        session.setAttribute(EycConstants.VERIFY_CODE, verifyCode.toLowerCase(Locale.getDefault()));
        // 生成图片
        int w = 250;
        int h = 80;
        VerifyCodeUtil.outputImage(w, h, response.getOutputStream(), verifyCode);
    }

    @RequestMapping(value = {"home"})
    public String home(Model model) {
        model.addAttribute("eycUser", new EycUser());
        return "home";
    }

    @RequestMapping("index")
    public String index(Model model) {
        return "index";
    }
    
    @RequestMapping(value = {"/", "/login"})
    public String login(HttpServletRequest request, HttpServletResponse response, Model model) {
        model.addAttribute("isAjaxRequest", Boolean.FALSE);
        return "login";
    }


}
